package com.lili.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfig {

    /**
     * 配置跨域过滤器
     * @return
     */
    @Bean
    public CorsWebFilter corsWebFilter() {


        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();

        CorsConfiguration config = new CorsConfiguration();
        // 设置允许跨域的条件
        config.setAllowCredentials(false);
        config.addAllowedOrigin("*");
        config.addAllowedMethod("*");
        config.addAllowedHeader("*");
        config.setMaxAge(36000L);

        configSource.registerCorsConfiguration("/**", config);
        // 创建跨域拦截器
        CorsWebFilter filter = new CorsWebFilter(configSource);

        return filter;
    }

}
